package com.study.blogsearchconsumer.controller;

import com.github.pagehelper.PageInfo;
import com.study.blogsearchconsumer.service.SearchFeignService;
import com.study.blogsearchpojo.entity.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description:
 * @author: yana.jiang
 * @date: 2022/1/18
 */
@Controller
public class HomeController {
    @Autowired
    private SearchFeignService searchFeignService;
    @RequestMapping("/")
    public ModelAndView home(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
    @RequestMapping("/searchBlog")
    public String elasticSearch(Model model, @RequestParam(value="keywords")String keywords,@RequestParam(value="pageNo",defaultValue="1")int pageNo){

        List<Blog> blogList = searchFeignService.search("blog_search_yana", keywords,pageNo);
        PageInfo pageInfo = new  PageInfo(blogList,10);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("keywords",keywords);
        return "search/blog_search";
    }
}
